package com.zq.vaccinum.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Objects;

/*
* Json格式的数据进行响应
* */
public class JsonResult extends LinkedHashMap<String, Object> implements Serializable {


    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回信息
     */
    public static final String MESSAGE_TAG = "message";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    // 状态码
    private Integer code;
    // 描述信息
    private String message;

    HashMap<String, Object> map = new HashMap<>();
    /**
     * 链式存储
     * @param key
     * @param value
     * @return
     */
    @Override
    public JsonResult put(String key, Object value) {
        map.put(key,value);
        super.put(DATA_TAG,map);
        return this;
    }

    /**
     * 其它异常信息
     * @param e
     */
    public JsonResult(Throwable e) {
        super.put(MESSAGE_TAG, e.getMessage());
    }

    public static JsonResult getJsonResult(){
        return new JsonResult();
    }

    public JsonResult() {
    }

    public JsonResult(int code) {
        super.put(CODE_TAG,code);
    }

    public JsonResult(int code,String message) {
        this(code);
        super.put(MESSAGE_TAG,message);
    }

    public JsonResult(int code,Object data) {
        this(code);
        if (Objects.nonNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    public JsonResult(int code,String message,Object data) {
        this(code,message);
        if (Objects.nonNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
